package com.example.sunjian.myapplication.handle_img;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.sunjian.myapplication.R;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import id.zelory.compressor.Compressor;

/**
 * 作者：sunjian on 16/7/6 14:35
 * 邮箱：sunjian@cyhz.com
 */
public class HandleImageActivity extends Activity implements View.OnClickListener{


    private Button mBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.handle_img);
        mBtn=(Button)findViewById(R.id.hand_img);
        mBtn.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
//        compressBySize();
        compress();
//        handString();

    }

    public void compressBySize(){
        String path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator+"cyhz_zhihuangtongerqi"+"/test.jpg";
//        Bitmap bitmap=BitmapFactory.decodeFile(path);
//        int dens = bitmap.getDensity();
//        Log.e("sj","bitmap size:"+(bitmap.getByteCount())+"     dens:"+dens);

//        Matrix matrix = new Matrix();
//        matrix.postScale(1.5f,1.5f); //长和宽放大缩小的比例
//        Bitmap resizeBmp = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
//        Log.e("sj","bitmap2 size:"+(resizeBmp.getByteCount())+"     dens:"+resizeBmp.getDensity());


        String path1 = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator+"cyhz_zhihuangtongerqi"+"/test1.jpg";
        try {
            FileOutputStream fos = new FileOutputStream(new File(path1));
//            bitmap.compress(Bitmap.CompressFormat.JPEG,20,fos);

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize=4;
            options.outWidth = 100;
            options.outHeight=400;
            Bitmap bitmap1=BitmapFactory.decodeFile(path,options);
            int dens1 = bitmap1.getDensity();
            Log.e("sj","bitmap1 size:"+(bitmap1.getByteCount())+"     dens1:"+dens1  +"   width:"+bitmap1.getWidth()
            +"    height:"+bitmap1.getHeight());
            bitmap1.compress(Bitmap.CompressFormat.JPEG,100,fos);//尺寸不会改变，还是原图尺寸
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }


    public void compress(){
        String path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator+"cyhz_zhihuangtongerqi"+"/test.jpg";
        String path1 = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator+"cyhz_zhihuangtongerqi"+"/test1.jpg";
        Bitmap bitmap=BitmapFactory.decodeFile(path);


        String  photoPathSmall= Compressor.getDefault(this).compressToFile(new File(path)).getPath();
        Log.e("photoPathSmall","photoPathSmall----->>"+photoPathSmall);

        File file = new File(photoPathSmall);

        Log.e("photoPathSmall","photoPathSmall-length---->>"+file.length());

//        new Compressor.Builder(this).setCompressFormat(Bitmap.CompressFormat.JPEG)
//                .setMaxHeight(640).setMaxWidth(480).setQuality(20).setDestinationDirectoryPath(path1)
//                .build().compressToFile(new File(path));

    }


    public void handString(){

//        StringBuilder sb = new StringBuilder("@13452 @13452 @1234");
//        int index = sb.indexOf("13452");
//        if(index!=-1){
//            Log.e("sj","index:"+index+"    sb:"+sb.toString());
//            sb.replace(index,"13452".length()+index,"测试一下");
//             index = sb.indexOf("13452");
//            Log.e("sj","index:"+index+"    sb:"+sb.toString());
//            sb.replace(index,"13452".length()+index,"测试一下");
//
//
//        }
        String str = "@李爱华 gf@577374 ggg@8";
        str = str.replace("@577374 ","@车晓儿 ");

        mBtn.setText(str);
    }


}
